# -*- encoding=utf8 -*-
__author__ = "chen"

from datetime import datetime
from common.common_util import *

def get_yabiao_task(hwnd):
    # 获取押镖 任务弹窗
    # 找到押镖，返回坐标，否则返回空
    connect_device(f"Windows:///{hwnd}")
    try:
        pcos = wait(Template(r"yabiao_task_putong.png", record_pos=(0.33, 0.1), resolution=(635, 503)), 1)
        if pcos:
            app_touch(pcos, [-20, 20], [-5, 5])
    except :
        print("未找到押镖任务弹窗")
        return None
def gaoji_yaobiao(idx):
    '''
    高级押镖
    :param idx: 设备序列号
    :return:
    '''
    connect_device(f'Android:///{idx}')
    #点击 高级押镖
    try:
        pcos = wait(Template(r"tpl1717680264268.png", record_pos=(0.332, 0.154), resolution=(494, 398)),600)
        if pcos:
            app_touch(pcos, [-20, 20], [-2, 2])
        pcos = wait(Template(r"sure_ok.png", record_pos=(0.091, 0.051), resolution=(635, 503)))
        if pcos:
            app_touch(pcos, [-5, 5], [-2, 2])
        return True
    except:
        filename = os.path.join(base_path, "yabiao", "sreenshot", f"{idx}-test_gaoji_yaobiao.png")
        snapshot(filename=filename, msg=f"{idx} 任务结束")
        return False
def putong_yaobiao(idx):
    '''
    普通押镖
    :param idx: 设备序列号
    :return:
    '''
    connect_device(f'Android:///{idx}')
    # 点击 普通押镖
    try:
        pcos = wait(Template(r"tpl1717680055268.png", record_pos=(0.332, 0.097), resolution=(494, 398)))
        if pcos:
            app_touch(pcos, [-20, 20], [-2, 2])
    # 点击确定
        pcos = wait(Template(r"sure_ok.png", record_pos=(0.091, 0.051), resolution=(635, 503)))
        if pcos:
            app_touch(pcos, [-20, 20], [-2, 2])
        return True
    except:
        filename = os.path.join(base_path, "yabiao", "sreenshot", f"{idx}-test_putong_yaobiao.png")
        snapshot(filename=filename, msg=f"{idx} 任务结束")
        return False
def run_task(idx,is_gj=0):
    '''
    押镖任务
    :param idx: 设备序列号
    :param is_gj: 判断是否高级押镖，# 1:高级押镖和普通押镖，0：普通押镖
    :return:
    '''

    if is_gj == 1:
        gj_counter = 0
        pt_counter = 0
        while ((3 > gj_counter) and (3> pt_counter)):
            connect_device(f'Android:///{idx}')
            var_gj = gaoji_yaobiao(idx)
            if var_gj:
                gj_counter = gj_counter + 1
            var_pt = putong_yaobiao(idx)
            if var_pt:
                pt_counter = pt_counter + 1
    else:
        pt_counter = 0
        while (3 > pt_counter):
            connect_device(f'Android:///{idx}')
            var_pt = putong_yaobiao(idx)
            if var_pt:
                pt_counter = pt_counter + 1


if __name__ == '__main__':
    # part1 获取有效设备
    get_json = get_config()
    device_list = get_json["dev"] # 设备列表
    is_gj =get_json["is_gjyabiao"] # 1:高级押镖和普通押镖，0：普通押镖

    # part2 执行押镖
    for i in range(len(device_list)):
        print(device_list[i])
        p = Process(target=run_task,args=(device_list[i],is_gj,))
        p.start()

